package com.letv.whatslive.model.redis.chat.protocol;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.codehaus.plexus.util.StringUtils;

import java.io.Serializable;

/**
 * Created by wangjian7 on 2016/1/18.
 */
public class DoubleLiveEvent implements Serializable {

    /**
     * 协议版本：
     * 2016-01-18 初始值是修改为230
     */
    private int version = 230;
    private String uid; //主播ID
    private String uPic; //主播头像
    private String uName; //主播昵称
    private int uRole; //主播角色
    private String inviteUid; //被邀请者ID
    private String inviteUname; //被邀请者昵称
    private String pId; //直播ID
    private String pName; //直播标题
    private String doc; //文案，用于APP显示邀请XXX主播、正在等待其他人加入等消息
    private String streamId; //主屏幕StreamId
    /**
     * 消息类型：1.主播发起邀请;2.副主播接受邀请;3.副主播拒绝邀请;4.主播中断邀请;5.副主播推流成功;6.主播切换屏幕;
     *      7.副主播结束直播;8.主播结束副主播的直播;9.主播结束直播;10被邀请者设备不支持
     */
    private int action;
    private long date; //日期，精确到毫秒时间戳

    public DoubleLiveEvent() {

    }

    public DoubleLiveEvent(String uid, String inviteUid, String pId, int aciton) {
        this.uid = uid;
        this.inviteUid = inviteUid;
        this.pId = pId;
        this.action = aciton;
        this.date = System.currentTimeMillis();
    }

    public DoubleLiveEvent(String uid, String uName, String uPic, int uRole, String inviteUid, String inviteUname, String pId, String pName, String doc, int action) {
        this.uid = uid;
        this.uName = uName;
        this.uPic = uPic;
        this.uRole = uRole;
        this.inviteUid = inviteUid;
        this.inviteUname = inviteUname;
        this.pId = pId;
        this.pName = pName;
        this.doc = doc;
        this.action = action;
        this.date = System.currentTimeMillis();
    }

    public DoubleLiveEvent(String json) {
        JSONObject jsonObject = JSON.parseObject(json);
        this.date = jsonObject.getLong("date");
        this.uid = jsonObject.getString("uid");
        this.uName = jsonObject.getString("uName");
        this.uPic = jsonObject.getString("uPic");
        this.uRole = jsonObject.getInteger("uRole");
        this.inviteUid = jsonObject.getString("inviteUid");
        this.inviteUname = jsonObject.getString("inviteUname");
        this.pId = jsonObject.getString("pId");
        this.pName = jsonObject.getString("pName");
        this.doc = jsonObject.getString("doc");
        this.action = jsonObject.getInteger("action");
        this.streamId = jsonObject.getString("streamId");
        this.version = jsonObject.getInteger("version");
    }

    public static DoubleLiveEvent createDoubleLiveEvent(String uid, String inviteUid, String pId, int action) {
        DoubleLiveEvent event = new DoubleLiveEvent();
        event.setUid(uid);
        event.setInviteUid(inviteUid);
        event.setpId(pId);
        event.setAction(action);
        event.setDate(System.currentTimeMillis());
        return event;
    }

    public String toLogString(){
        StringBuffer sb = new StringBuffer();
        sb.append("DoubleLiveEvent{");
        sb.append("uid=").append(uid);
        sb.append(", inviteUid=").append(inviteUid);
        sb.append(", pId=").append(pId);
        sb.append(", action=").append(action);
        sb.append(", streamId=").append(streamId).append("");
        sb.append(", version='").append(version).append("'");
        sb.append(", date='").append(date).append("'");
        return sb.toString();
    }

    public boolean checkData(){
        if(StringUtils.isBlank(this.uid) || StringUtils.isBlank(this.inviteUid) || StringUtils.isBlank(this.pId)){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 判断是否是主播发起的消息
     * @return
     */
    public boolean checkIsHostMessage(){
        if(1==this.action || 4==this.action || 6==this.action || 8==this.action || 9==this.action){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getuPic() {
        return uPic;
    }

    public void setuPic(String uPic) {
        this.uPic = uPic;
    }

    public String getuName() {
        return uName;
    }

    public void setuName(String uName) {
        this.uName = uName;
    }

    public int getuRole() {
        return uRole;
    }

    public void setuRole(int uRole) {
        this.uRole = uRole;
    }

    public String getInviteUid() {
        return inviteUid;
    }

    public void setInviteUid(String inviteUid) {
        this.inviteUid = inviteUid;
    }

    public String getInviteUname() {
        return inviteUname;
    }

    public void setInviteUname(String inviteUname) {
        this.inviteUname = inviteUname;
    }

    public String getpId() {
        return pId;
    }

    public void setpId(String pId) {
        this.pId = pId;
    }

    public String getpName() {
        return pName;
    }

    public void setpName(String pName) {
        this.pName = pName;
    }

    public String getDoc() {
        return doc;
    }

    public void setDoc(String doc) {
        this.doc = doc;
    }

    public String getStreamId() {
        return streamId;
    }

    public void setStreamId(String streamId) {
        this.streamId = streamId;
    }

    public int getAction() {
        return action;
    }

    public void setAction(int action) {
        this.action = action;
    }

    public long getDate() {
        return date;
    }

    public void setDate(long date) {
        this.date = date;
    }
}
